package Server.Game;

import java.util.ArrayList;

import Server.Sessions.OutboundMessages;

import shared.Messages.DcWinMessage;
import shared.Messages.MoveMessage;
import shared.Messages.QuestionMessage;
import shared.Messages.WinMessage;
import shared.Player.Player;
import shared.Tiles.Tile;

public class GameOK extends Thread {
	OutboundMessages outQueue;
	Player p0;
	Player p1;
	Player p2;
	Player winner;
	int GameNo;
	int rollDice;
	TurnController tc;
	int turnPlayerId;
	ArrayList<Tile> tiles;
	boolean moved;
	boolean dcBreak = false;
	QuestionGetter questionGetter = new QuestionGetter();

	public GameOK(OutboundMessages outQueue, int GameNo, Player p0, Player p1,
			Player p2, ArrayList<Tile> tiles) {
		this.outQueue = outQueue;
		this.GameNo = GameNo;
		this.p0 = p0;
		this.p1 = p1;
		this.p2 = p2;
		this.tiles = tiles;
		tc = new TurnController(outQueue, GameNo);
		turnPlayerId = tc.nextTurn();
		start();
	}

	public void run() {
		while (true) {
			moved = false;
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				System.out.println("Sleep kaktestatud!");
				break;
			}
			switch (turnPlayerId) {
			case 1:
				if (p0.rolled) {
					p0.move(tiles);
			
						outQueue.addMessage(new MoveMessage(p0.rollRes,
								p0.current.tileId, GameNo, p0.playerID));
					
					if (!p0.anwsered) {
						p0.question = questionGetter.getQuestion();
						outQueue.addMessage(new QuestionMessage(p0.question[0],
								GameNo, p0.playerID));
						while (true) {
							if (p0.anwsered) {
								moved = true;
								break;
							}
						}
					} else {
						moved = true;
					}
				}
				break;
			case 2:
				if (p1.rolled) {
					p1.move(tiles);
						outQueue.addMessage(new MoveMessage(p1.rollRes,
								p1.current.tileId, GameNo, p1.playerID));
					if (!p1.anwsered) {
						p1.question = questionGetter.getQuestion();
						outQueue.addMessage(new QuestionMessage(p1.question[0],
								GameNo, p1.playerID));
						while (true) {
							if (p1.anwsered) {
								moved = true;
								break;
							}
						}
					} else {
						moved = true;
					}
				}
				break;
			case 3:
				if (p2.rolled) {
					p2.move(tiles);
						outQueue.addMessage(new MoveMessage(p2.rollRes,
								p2.current.tileId, GameNo, p2.playerID));
					if (!p2.anwsered) {
						p2.question = questionGetter.getQuestion();
						outQueue.addMessage(new QuestionMessage(p2.question[0],
								GameNo, p2.playerID));
						while (true) {
							if (p2.anwsered) {
								moved = true;
								break;
							}
						}
					} else {
						moved = true;
					}
				}
				break;
			}
			if(p0.dc || p1.dc || p2.dc){
				if(p0.current.tileId > p1.current.tileId && p0.current.tileId > p2.current.tileId){
					outQueue.addMessage(new DcWinMessage(GameNo, p0.name));
				}else if(p1.current.tileId > p0.current.tileId && p1.current.tileId > p2.current.tileId){
					outQueue.addMessage(new DcWinMessage(GameNo, p1.name));
				}else if(p2.current.tileId > p1.current.tileId && p2.current.tileId > p0.current.tileId){
					outQueue.addMessage(new DcWinMessage(GameNo, p2.name));
				}else{
					outQueue.addMessage(new DcWinMessage(GameNo, "Viik"));
				}
				dcBreak = true;
				break;
			}
			if (p0.current.tileId == 19) {
				winner = p0;
				break;
			} else if (p1.current.tileId == 19) {
				winner = p1;
				break;
			} else if (p2.current.tileId == 19) {
				winner = p2;
				break;
			}
			if (moved) {
				turnPlayerId = tc.nextTurn();
			}
		}
		if(!dcBreak){
			outQueue.addMessage(new WinMessage(winner.name, GameNo));
		}
	}
}
